using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;

namespace DAL
{
  public class DoanhNghiepDAL
    {
          private SqlConnection cn = null;
        public DoanhNghiepDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (DoanhNghiep dn)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDN",dn.tendn);
           cm.Parameters.AddWithValue("@DiaChi",dn.diachi);
           cm.Parameters.AddWithValue("@DienThoai",dn.dienthoai);
           cm.Parameters.AddWithValue("@Email",dn.email);
           cm.Parameters.AddWithValue("@TaiKhoan",dn.taikhoan);
           cm.Parameters.AddWithValue("@MatKhau",dn.matkhau);
          


            
           
            cm.ExecuteNonQuery();
        }
        public void Update(DoanhNghiep dn)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", dn.madn);
          cm.Parameters.AddWithValue("@TenDN",dn.tendn);
           cm.Parameters.AddWithValue("@DiaChi",dn.diachi);
           cm.Parameters.AddWithValue("@DienThoai",dn.dienthoai);
           cm.Parameters.AddWithValue("@Email",dn.email);
           cm.Parameters.AddWithValue("@TaiKhoan",dn.taikhoan);
           cm.Parameters.AddWithValue("@MatKhau",dn.matkhau);
          
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", iD);
            cm.ExecuteNonQuery();
        }

        public tbDoanhNghiepCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDoanhNghiepCollection dnCollection = new tbDoanhNghiepCollection();

            while (dr.Read())
            {
                DoanhNghiep doanhnghiep = new DoanhNghiep();
                doanhnghiep.madn = int.Parse(dr.GetValue(0).ToString());
                doanhnghiep.tendn = dr.GetValue(1).ToString();
                doanhnghiep.diachi = dr.GetValue(2).ToString();
                doanhnghiep.dienthoai = dr.GetValue(3).ToString();
                doanhnghiep.email = dr.GetValue(4).ToString();
                doanhnghiep.taikhoan = dr.GetValue(5).ToString();
                doanhnghiep.matkhau = dr.GetValue(6).ToString();



                dnCollection.Add(doanhnghiep);


             
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dnCollection;
        }
    }
    
}
